#include "sys_config.h"

/**
 * @brief 蜂鸣器状态更新
 * @note 无
 * @param new_state 蜂鸣器的新状态
 * @return {*}
 */
void Control_Beep_Update(te_beep_status_t new_state)
{
    if(BEEP_STATUS_ON == new_state){
        set_buzzer(true);
    }else{
        set_buzzer(false);
    }
}

/**
 * @brief 三色灯状态更新
 * @note 无
 * @param new_state 三色灯的新状态
 * @return {*}
 */
void Control_Led_Update(te_led_status_t new_state)
{
    if(LED_STATUS_ON == new_state){
        AW2013_Control_Red(255);
        AW2013_Control_Green(255);
        AW2013_Control_Blue(255);
    }else{
        AW2013_Control_Red(0);
        AW2013_Control_Green(0);
        AW2013_Control_Blue(0);
    }
}

/**
 * @brief 小车手动控制
 * @note 从globals_cmd_now获取小车状态; 发送对应JSON命令给单片机; 更新蜂鸣器与LED灯状态
 * @param {*}
 * @return {*}
 */
void Control_Car_Manual(void)
{
    if(1 == globals_cmd_now.flag_cmd_update){
        globals_cmd_now.flag_cmd_update = 0;
        printf("update to usart : %d\r\n", globals_cmd_now.state_cmd);
        Json_Enc_SendToUart(globals_cmd_now.state_cmd, "car", globals_cmd_now.speed);
        Control_Beep_Update(globals_cmd_now.beep_cmd);
        Control_Led_Update(globals_cmd_now.led_cmd);
    }
}

/**
 * @brief 无人驾驶（自动避障）
 * @note 从globals_car_status获取距离; 发送对应JSON命令给单片机
 * @param {*}
 * @return {*}
 */
void Control_Car_Driveless(void)
{
    globals_cmd_now.flag_cmd_update = 0;
    while(0 == globals_cmd_now.flag_cmd_update){
        if(globals_car_status.distance < 300){
            Json_Enc_SendToUart(DRIVING_STATUS_RUN, "car", globals_cmd_now.speed);
        }else{
            Json_Enc_SendToUart(DRIVING_STATUS_LEFT, "car", globals_cmd_now.speed);
        }
    }

}

/**
 * @brief 自动循迹
 * @note 扩展接口
 * @param {*}
 * @return {*}
 */
void Control_Car_AutoTarck(void)
{
    if(1 == globals_cmd_now.flag_cmd_update){
        globals_cmd_now.flag_cmd_update = 0;
        printf("update to usart : %d,beep %d, led %d\r\n", globals_cmd_now.state_cmd, globals_cmd_now.beep_cmd, globals_cmd_now.led_cmd);
        Json_Enc_SendToUart(globals_cmd_now.state_cmd, "car", globals_cmd_now.speed);
        Control_Beep_Update(globals_cmd_now.beep_cmd);
        Control_Led_Update(globals_cmd_now.led_cmd);
    }
}

/**
 * @brief 确定小车形式模式
 * @note 从globals_cmd_now获取命令; 进行控制
 * @param {*}
 * @return {*}
 */
void Control_Car_DrivingMode(void)
{
    printf("***********************\n");
    switch (globals_cmd_now.mode_cmd)
    {
    case DRIVING_MODE_MANUAL:
        globals_car_status.driving_mode = DRIVING_MODE_MANUAL;
        Control_Car_Manual();
        break;
    
    case DRIVING_MODE_DRIVELESS:
        globals_car_status.driving_mode = DRIVING_MODE_DRIVELESS;
        Control_Car_Driveless();
        break;
    
    case DRIVING_MODE_AUTO_TARCK:
        globals_car_status.driving_mode = DRIVING_MODE_AUTO_TARCK;
        Control_Car_AutoTarck();
        break;
    
    default:
        break;
    }
}

/**
 * @brief 确定设置小车或者获取小车状态
 * @note 从globals_cmd_now获取命令; 进行控制
 * @param {*}
 * @return {*}
 */
void Control_Car(void)
{
    switch (globals_cmd_now.command)
    {
    case COMMAND_GET:
        /* code */
        break;
    case COMMAND_SET:
        Control_Car_DrivingMode();
        break;
    
    default:
        break;
    }
}

/**
 * @brief 小车控制任务
 * @note 处理连接请求，接收数据，进行JSON解析后，存入全局变量globals_cmd_now; 发送对应指令给单片机
 * @param {*}
 * @return {*}
 */
void Car_Control_Tack(void)
{
    while(1){
        // switch (globals_cmd_now.device_id)
        // {
        // case DEVICE_CAR:
        //     Control_Car();
        //     break;
        // case DEVICE_BEEP:
        //     break;
        // case DEVICE_CAMERA:
        //     break;
        // default:
        //     break;
        // }
        usleep(300000);
       // osMutexAcquire(globals_Mutex_ID, osWaitForever);
        Control_Car();
       // osMutexRelease(globals_Mutex_ID);
    }
}

